package com.returnsoft.resource.service;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import com.returnsoft.resource.dto.TrainingDto;
import com.returnsoft.resource.eao.TrainingEao;
import com.returnsoft.resource.entity.Training;
import com.returnsoft.resource.exception.ConversionExcepcion;
import com.returnsoft.resource.exception.EaoExcepcion;
import com.returnsoft.resource.exception.RecruitmentException;
import com.returnsoft.resource.util.Conversion;

/**
 * Session Bean implementation class TrainingBean
 */
@Stateless
@LocalBean
public class TrainingBean implements TrainingInterface {

	@EJB
	private TrainingEao eao;

	@EJB
	private Conversion conv;

	/**
	 * Default constructor.
	 */
	public TrainingBean() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<TrainingDto> findList(List<Integer> areasId,
			List<Integer> subAreasId, Integer recruiterId,String trainingAt,
			Integer stateId, Integer trainerId, String candidateDocumentNumber,
			String candidateFirstname, String candidateLastname)
			throws RecruitmentException {

		try {

			List<TrainingDto> entities = eao.findList(areasId, subAreasId,recruiterId,trainingAt, stateId,
					trainerId, candidateDocumentNumber, candidateFirstname,
					candidateLastname);

			return entities;
		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}

	}

	@Override
	public TrainingDto add(TrainingDto dto) throws RecruitmentException {
		try {

			Training entity = conv.toTraining(dto);
			entity = eao.add(entity);
			dto = conv.fromTraining(entity);

			return dto;

		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}
	}

	@Override
	public TrainingDto findById(Integer id) throws RecruitmentException {
		try {
			Training entity = eao.findById(id);
			TrainingDto dto = conv.fromTraining(entity);
			return dto;
		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}
	}
	
	@Override
	public List<TrainingDto> findPendingsByCandidateId(Integer candidateId)
			throws RecruitmentException {
		try {
			List<TrainingDto> trainings = eao
					.findPendingsByCandidateId(candidateId);
			return trainings;
		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}
	}

	

	/*@Override
	public List<TrainingDto> findByDocumentNumber(String documentNumber)
			throws RecruitmentException {
		try {
			List<TrainingDto> trainings = eao
					.findByDocumentNumber(documentNumber);

			return trainings;
		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}
	}*/

}
